// Copyright 2022 a.o.smith ALL Rights Reserved
package com.liuxinlong.api;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.naming.AuthenticationException;
import javax.naming.Context;
import javax.naming.directory.InitialDirContext;
import java.util.Hashtable;

/**
 * 用户登陆验证api
 *
 * @author liuxinlong@hotwater.com.cn
 * @version 1.0.0
 * @since 2022年8月26日
 */
@Component
public class LDAPAuth {
    /**
     * 连接AD域,验证账号密码是否正确
     *
     * @param username 用户名
     * @param password 密码
     */
    public boolean ady(String username, String password) {
        int local = username.indexOf("@");
        String userRegion = username.substring(local,username.length());
        String host = StringUtils.equals(userRegion,"@hotwater.com.cn") ? "172.30.109.196" : "172.30.109.163";
        String baseDn = StringUtils.equals(userRegion,"@hotwater.com.cn") ? "dc=wpcnj,dc=org":"dc=wtc-china,dc=org";
        InitialDirContext ctx = null;

//        String host = "172.30.100.96";
        String post = "389";
        Hashtable<String, String> hashtable = new Hashtable<String, String>();

        hashtable.put(Context.SECURITY_AUTHENTICATION, "simple"); // LDAP访问安全级别(none,simple,strong)

        hashtable.put(Context.SECURITY_PRINCIPAL, username); // AD的用户名
        hashtable.put(Context.SECURITY_CREDENTIALS, password); // AD的密码
        hashtable.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); // LDAP工厂类
        hashtable.put("com.sun.jndi.ldap.connect.timeout", "3000");// 连接超时设置为3秒
        hashtable.put(Context.PROVIDER_URL, "ldap://" + host + ":" + post);
        try {
            ctx = new InitialDirContext(hashtable);// 初始化上下文
            System.out.println("身份验证成功!");
            return true;
        } catch (AuthenticationException e) {
            System.out.println("身份验证失败!");
            e.printStackTrace();
            return false;
        } catch (javax.naming.CommunicationException e) {
            System.out.println("AD域连接失败!");
            e.printStackTrace();
            return false;
        } catch (Exception e) {
            System.out.println("身份验证未知异常!");
            e.printStackTrace();
            return false;
        } finally {
            if (null != ctx) {
                try {
                    ctx.close();
                    ctx = null;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 连接AD域,验证账号密码是否正确
     *
     * @param username 用户名(邮箱)
     * @param password 密码
     */
    public boolean adyOut(String username, String password) {
        int local = username.indexOf("@");
        String userRegion = username.substring(local,username.length());
        String host = StringUtils.equals(userRegion,"@hotwater.com.cn") ? "172.30.109.196" : "172.30.109.163";
        String baseDn = StringUtils.equals(userRegion,"@hotwater.com.cn") ? "dc=wpcnj,dc=org":"dc=wtc-china,dc=org";
        InitialDirContext ctx = null;

//        String host = "172.30.100.96";
        String post = "389";
        Hashtable<String, String> hashtable = new Hashtable<String, String>();

        hashtable.put(Context.SECURITY_AUTHENTICATION, "simple"); // LDAP访问安全级别(none,simple,strong)

        hashtable.put(Context.SECURITY_PRINCIPAL, username); // AD的用户名
        hashtable.put(Context.SECURITY_CREDENTIALS, password); // AD的密码
        hashtable.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); // LDAP工厂类
        hashtable.put("com.sun.jndi.ldap.connect.timeout", "3000");// 连接超时设置为3秒
        hashtable.put(Context.PROVIDER_URL, "ldap://" + host + ":" + post);
        try {
            ctx = new InitialDirContext(hashtable);// 初始化上下文
            System.out.println("身份验证成功!");
            return true;
        } catch (AuthenticationException e) {
            System.out.println("身份验证失败!");
            e.printStackTrace();
            return false;
        } catch (javax.naming.CommunicationException e) {
            System.out.println("AD域连接失败!");
            e.printStackTrace();
            return false;
        } catch (Exception e) {
            System.out.println("身份验证未知异常!");
            e.printStackTrace();
            return false;
        } finally {
            if (null != ctx) {
                try {
                    ctx.close();
                    ctx = null;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
